package com.evan.uyclub.SexTool;

//加载弹窗

import com.evan.uyclub.Other.BackgroundTask;
import com.evan.uyclub.Other.Utlis;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.transition.AutoTransition;
import androidx.transition.TransitionManager;

import com.evan.uyclub.R;
import com.evan.uyclub.yellow.PlayerActivity;
import com.google.android.material.button.MaterialButton;
import com.gyf.immersionbar.ImmersionBar;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.tapadoo.alerter.Alerter;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IptvActivity extends AppCompatActivity {

    @BindView(R.id.root)
    ViewGroup root;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.rv)
    RecyclerView rv;

    private HashMap<String, Object> map = new HashMap<>();
    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> listmap = new ArrayList<>();
    private String realUrl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iptv);
        ButterKnife.bind(this);

        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.appbarColor)
                .navigationBarColor(R.color.backgroundColor)
                .autoDarkModeEnable(true)
                .init();

        //返回
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(view -> {
            this.finish();
        });

        toolbar.setTitle(getString(R.string.IPTV电视直播));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(v -> onBackPressed());

        rv.setItemViewCacheSize(9999);

        if (!Utlis.isVPNConnected(IptvActivity.this)) {
            Utlis.LoadingDialog(IptvActivity.this);
            HttpRequest.build(IptvActivity.this, "https://gitee.com/x1602965165/DaiMeng/raw/master/iptv.txt")
//            HttpRequest.build(IptvActivity.this,"https://gitee.com/jfkjrym/uy-club/raw/master/iptx.txt")
                    .addHeaders("Charset", "UTF-8")
                    .addHeaders("User-Agent", WebSettings.getDefaultUserAgent(IptvActivity.this))
                    .skipSSLCheck()
                    .setResponseListener(new ResponseListener() {
                        @Override
                        public void onResponse(String response, Exception error) {
                            Log.e("TAG", response);
                            if (error == null) {
                                Utlis.loadDialog.dismiss();
                                try {
                                    map.clear();
                                    list.clear();
                                    listmap.clear();
                                    list = new ArrayList<String>(Arrays.asList(response.split("\n")));
                                    for (int i1 = 0; i1 < (int) (list.size()); i1++) {
                                        map = new HashMap<>();
                                        map.put("name", list.get(i1).split(",")[0]);
                                        Log.e("TAG", list.get(i1).split(",")[0]);
                                        map.put("url", list.get(i1).split(",")[1]);
                                        Log.e("TAG", list.get(i1).split(",")[0]);
                                        listmap.add(map);
                                    }
                                    TransitionManager.beginDelayedTransition(root, new AutoTransition());
                                    rv.setAdapter(new Recyclerview1Adapter(listmap));
                                    rv.getAdapter().notifyDataSetChanged();
                                } catch (Exception e) {
                                }
                            } else {
                                Utlis.loadDialog.dismiss();
                                Alerter.create((Activity) IptvActivity.this)
                                        .setTitle(R.string.温馨提示)
                                        .setText(R.string.加载失败)
                                        .setBackgroundColorInt(getResources().getColor(R.color.error))
                                        .show();
                            }


                        }
                    }).doGet();
        }

    }

    public class Recyclerview1Adapter extends RecyclerView.Adapter<Recyclerview1Adapter.ViewHolder> {
        ArrayList<HashMap<String, Object>> _data;

        public Recyclerview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
            _data = _arr;
        }

        @Override
        public @NotNull Recyclerview1Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater _inflater = (LayoutInflater) parent.getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View _v = _inflater.inflate(R.layout.item_button, null);
            RecyclerView.LayoutParams _lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            _v.setLayoutParams(_lp);
            return new ViewHolder(_v);
        }

        @Override
        public void onBindViewHolder(ViewHolder _holder, @SuppressLint("RecyclerView") final int _position) {
            View _view = _holder.itemView;
            final MaterialButton button1 = _view.findViewById(R.id.button1);
            button1.setText((CharSequence) _data.get((int) _position).get("name"));
            button1.setOnClickListener(v -> {
                Utlis.LoadingDialog(IptvActivity.this);
                Intent intent = new Intent();
                intent.putExtra("url", realUrl);
                intent.putExtra("title", String.valueOf(_data.get((int)_position).get("name")));
                intent.putExtra("islive", true);
                intent.setClass(IptvActivity.this, PlayerActivity.class);
                startActivity(intent);
                /*new BackgroundTask(IptvActivity.this) {
                    @Override
                    public void doInBackground() {
                        URL url = null;
                        try {
                            url = new URL(String.valueOf(_data.get((int) _position).get("url")));
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            Utlis.loadDialog.dismiss();
                            conn.getResponseCode();
                            realUrl = conn.getURL().toString();
                            conn.disconnect();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onPostExecute() {
//                        Uri uri = Uri.parse(String.valueOf(realUrl));
//                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                        startActivity(intent);
                        Intent intent = new Intent();
                        intent.putExtra("url", realUrl);
                        intent.putExtra("title", String.valueOf(_data.get((int)_position).get("name")));
                        intent.putExtra("islive", true);
                        intent.setClass(IptvActivity.this, PlayerActivity.class);
                        startActivity(intent);

                    }
                }.execute();*/

            });
        }

        @Override
        public int getItemCount() {
            return _data.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(View v) {
                super(v);
            }
        }

    }
}